import 'package:mall/utils/broadcast/broadcast_util.dart';
import 'package:mall/utils/sc/state_controller.dart';

import '../model/order_detail_entity.dart';
import '../model/order_list_entity.dart';
import '../service/mine_service.dart';

// 订单详情
class OrderDetailSc extends ModelSC<OrderDetailEntity> {
  int? orderId;

  @override
  Future<OrderDetailEntity?> loadData() async{
    if (orderId == null){
      throw ArgumentError('orderId 不能为空');
    }

    final res = await MineService.fetchOrderDetail(orderId!);
    return res;
  }
}

// 订单列表
class OrderListSc extends PagingListSC<OrderEntity> implements OrderEvent{
  int orderType = 0;

  @override
  void onInit() {
    super.onInit();
    BroadcastUtil.register<OrderEvent>(this);

  }

  @override
  void onClose() {
    super.onClose();
    BroadcastUtil.unregister<OrderEvent>(this);
  }

  @override
  Future<List<OrderEntity>> loadData() async{
    final res = await MineService.fetchOrders(orderType, page, limit);
    count = res.total;
    return res.list ?? [];
  }
  
  @override
  void cancelOrder(int orderId) {

  }
  
  @override
  void deleteOrder(int orderId) {
    //找到这个订单
    final index = data.indexWhere((element) => element.id == orderId);
    if(index != -1){
      data.removeAt(index);
    }
  }
}


